package komorki;

import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;

public class Komorka extends JButton{
	private static final long serialVersionUID = 1L;
    private Neighbours neighbours;
	private Color currentColor = Color.WHITE;
	private Color transitionalColor = Color.WHITE;
	private int row;
	private int column;
	private int x;
	private int y;
	private static int count = 0;
	private int nr;
	
	public Komorka(int x, int y, int row, int column){
		super();
		this.setBounds(x, y, 30, 30);
		this.x = x;
		this.y = y;
		this.setBackground(Color.WHITE);
		this.column = column;
		this.row = row;
		nr = count++;
		
	
	}
	
	
	public void setColor(Color c){
		transitionalColor = c;
	}
	
	public void setDefinitiveColor(Color c){
		transitionalColor = currentColor = c;
	}
	public void buildNeighbourhood(){
		neighbours = new Neighbours();
		for(int i = row - 1 ;i <= row + 1; i++)
			for(int j = column -1 ; j <= column + 1; j++){
				if( !(i == row && j == column )){
					neighbours.add(i, j);
				}
			}
	}
	
	
	public void validate(){
		currentColor = transitionalColor;

	}
	
	public void changeNeighbourState(Color fromColor,Color toColor){
		Iterator<Komorka> ite = neighbours.iterator();
		Komorka k;
		while(ite.hasNext()){
			k = ite.next();
			if(k.equals(fromColor)){
				k.setColor(toColor);
				break;
			}
		}
	}
	
	public boolean equals(Object o){
		if(o instanceof Color){
			Color col = (Color)o;
			if(col.equals(currentColor))
				return true;
		}
		
		return false;
	}
	public HashMap<Color, Integer> map(){
		return neighbours.map();
	}
	public String toString(){
		return "Komorka ma nr " + nr;
	}
	

	public Color getColor() {
		return currentColor;
	}
	
	
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}

	
}
